var $ = require("../../utils/Ajax.js");
var WxParse = require('../../utils/wxParse/wxParse.js');
function GetTeaDetails(that,Id,GoodsId,Index){
  $.Ajax("api/goods", "GET", {
    teaId: Id,
    id: GoodsId,
    s: Index
  }, function (res) {
    if (res.s) {
      WxParse.wxParse('article', 'html', res.d.goods.details, that, 0);
      that.setData({
        TeaDetails: res.d.goods,
        StockNumber: res.d.goods.tea.stock
      })
    }else{
      wx.showToast({
        title: res.m,
        icon: 'success',
        duration: 1500
      })
      setTimeout(function () {
        wx.navigateBack({
          datail: 1
        })
      }, 1500)
    }
  })
}
Page({
  data: {
    // 此ID只在转卖接口中的使用
    Id: null,
    PictureUrl: getApp().PictureUrl,
    // 控制页面上下滚动
    ScrollY: true,
    BgModule: false,
    BgModuleSpeed: 300,
    BgModule1: false,
    BgModuleSpeed1: 300,
    BgModule2: false,
    BgModuleSpeed2: 300,

    // 用户信息是否显示
    UserInforBox: false,

    // Resell - 转卖按钮
    Resell: true,
    ResellMoney: 0,

    // SelfPromotion - 自提按钮
    SelfPromotion: true,

    // Send - 寄出按钮
    Send: true,

    // 商品信息
    TeaDetails: null,

    //可转卖数量
    StockNumber: 0,
    StockNumber_: 0,

    // 寄出人信息
    MailName: null,
    MailPhone: null,
    MailAddress: null
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "存茶详情"
    })
    console.log(options.Id)
    this.setData({
      Id: options.Id
    })
    GetTeaDetails(this, options.Id, options.GoodsId, options.Type);
    switch (Number(options.Type)){
      case 0:
        this.setData({
          Resell: false,
          SelfPromotion: false,
          Send: false
        })
      break;
      case 1:
        this.setData({
          Resell: true,
          SelfPromotion: true,
          Send: true
        })
      break;
      case 2:
        this.setData({
          Resell: true,
          SelfPromotion: true,
          Send: true
        })
      break;
    }
    
  },

  // 点击查看资料
  Data: function (e) {
    var Id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../Data/Data?Id=' + Id,
    })
  },

  // 输入转卖金额
  ResellMoney: function(e){
    var ResellMoney = e.detail.value;
    this.setData({
      ResellMoney: ResellMoney
    })
  },

  // 点击转卖
  Resell:function(e){
    var that = this
    var BgModuleSpeed = that.data.BgModuleSpeed;
    var BgModule = e.currentTarget.dataset.bgmodule;
    var Type = e.currentTarget.dataset.type;
    if(BgModule == "open"){
      that.setData({
        BgModule: true,
        ScrollY: false
      });
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(1).scale(1, 1).step();
      that.setData({
        animationData: animation.export()
      })
    } else {
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(0).scale(2, 2).step();
      that.setData({
        animationData: animation.export()
      })
      setTimeout(function () {
        that.setData({
          BgModule: false,
          ScrollY: true
        });
      }, BgModuleSpeed)
    }
    if (Type == 1){
      $.Ajax("api/addMySaleTea","POST",{
        id: that.data.Id,
        num: that.data.StockNumber_,
        price: that.data.ResellMoney
      },function(res){
        if(res.s){
          wx.showModal({
            content: '转卖信息已提交，等待客服审核上架销售',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          });
        }else{
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 1500
          })
        }
      })
    }
  },

  // 点击减少转卖数量
  StockJian: function(){
    var StockNumber_ = this.data.StockNumber_;
    if (StockNumber_ == 0){
      wx.showToast({
        title: '不能再减了',
        icon: 'none',
        duration: 1500
      })
    }else{
      --StockNumber_;
      this.setData({
        StockNumber_: StockNumber_
      })
    }
  },

  // 点击增加转卖数量
  StockJia: function(){
    var StockNumber = this.data.StockNumber;
    var StockNumber_ = this.data.StockNumber_;
    if (StockNumber == StockNumber_){
      wx.showToast({
        title: '不能再加了',
        icon: 'none',
        duration: 1500
      })
    }else{
      ++StockNumber_;
      this.setData({
        StockNumber_: StockNumber_
      })
    }
  },

  // 点击自提
  SelfPromotion: function(e){
    var that = this
    var Type = e.currentTarget.dataset.type;
    var BgModuleSpeed = that.data.BgModuleSpeed1;
    var BgModule = e.currentTarget.dataset.bgmodule;
    if (BgModule == "open") {
      that.setData({
        BgModule1: true,
        ScrollY: false
      });
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(1).scale(1, 1).step();
      that.setData({
        animationData1: animation.export()
      })
    } else {
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(0).scale(2, 2).step();
      that.setData({
        animationData1: animation.export()
      })
      setTimeout(function () {
        that.setData({
          BgModule1: false,
          ScrollY: true
        });
      }, BgModuleSpeed)
    }
    if (Type == 1) {
      $.Ajax("api/SelfMention", "POST", {
        tid: that.data.Id,
        num: that.data.StockNumber_
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'success',
          duration: 1500
        })
      })
    }
  },

  // 寄出人姓名
  MailName: function(e){
    this.setData({
      MailName: e.detail.value
    })
  },

  // 寄出人手机
  MailPhone: function (e) {
    this.setData({
      MailPhone: e.detail.value
    })
  },

  // 寄出人收货人地址
  MailAddress: function (e) {
    this.setData({
      MailAddress: e.detail.value
    })
  },

  // 点击寄出
  Send: function(e){
    var that = this
    var Type = e.currentTarget.dataset.type;
    var BgModuleSpeed = that.data.BgModuleSpeed2;
    var BgModule = e.currentTarget.dataset.bgmodule;
    if (BgModule == "open") {
      that.setData({
        BgModule2: true,
        ScrollY: false
      });
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(1).scale(1, 1).step();
      that.setData({
        animationData2: animation.export()
      })
    } else {
      var animation = wx.createAnimation({
        duration: BgModuleSpeed
      });
      animation.opacity(0).scale(2, 2).step();
      that.setData({
        animationData2: animation.export()
      })
      setTimeout(function () {
        that.setData({
          BgModule2: false,
          ScrollY: true
        });
      }, BgModuleSpeed)
    }
    if (Type == 1) {
      var MailName = that.data.MailName;
      var MailPhone = that.data.MailPhone;
      var MailAddress = that.data.MailAddress;
      if (MailName == '' || MailName == null || MailPhone == '' || MailPhone == null || MailAddress == '' || MailAddress == null){
        wx.showToast({
          title: '必填信息不能为空',
          icon: 'none',
          duration: 1500
        })
      }else{
        $.Ajax("api/mailMyTea", "POST", {
          tid: that.data.Id,
          num: that.data.StockNumber_,
          name: MailName,
          mobile: MailPhone,
          address: MailAddress
        }, function (res) {
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 1500
          })
        })
      }
    }
  }
})